﻿using System;
using UnityEngine;

namespace Yoozoo.Gameplay.City
{
    [ExecuteInEditMode]
    public class CityPoint_Lod0 : MonoBehaviour
    {
        public int id;

        public CityPointType pointType;

        public float radius;

        private GameObject mark;
        private CityPointType lastMarkType;

        private void Start()
        {
            for (int i = 0; i < this.transform.childCount; i++)
                GameObject.DestroyImmediate(this.transform.GetChild(i).gameObject);
        }

        private void OnDrawGizmos()
        {
            var point = CityPointEditor_Lod0.Instance.GetPointData(this.pointType);
            if (this.mark != null && point.pointType != this.lastMarkType)
            {
                GameObject.DestroyImmediate(this.mark);
                this.mark = null;
            }

            if (this.mark == null)
            {
                this.mark = GameObject.Instantiate(point.mark, this.transform);
                this.lastMarkType = point.pointType;
            }

            Gizmos.color = point.color;
            Gizmos.DrawWireSphere(this.transform.position, this.radius);
        }

        private void OnValidate()
        {
            transform.name = pointType.ToString() + "-" + id.ToString();
        }
    }
}